package com.yuyz.count;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.yuyz.util.CountUtil;

import java.util.List;

public class MonitorCountRun implements Runnable{

    /**
     * 分别计算methodA、methodB、methodC的TP90、TP99值
     */
    @Override
    public void run() {

        System.out.println("接口的响应性能监控分析 start");

        List<Long> methodListA = CountUtil.methodListA;// 方法A调用时间的集合
        List<Long> methodListB = CountUtil.methodListB;// 方法B调用时间的集合
        List<Long> methodListC = CountUtil.methodListC;// 方法C调用时间的集合

        if (methodListA.size() == 0) {
            System.out.println("methodA未调用，暂不处理！");
        } else if  (methodListA.size() > 1024) {
            methodListA.clear();
        } else {
            Double methodA_T90 = (methodListA.size() - 1) * 0.9;
            Double methodA_T99 = (methodListA.size() - 1) * 0.99;

            // 排序
            CollectionUtils.sort(methodListA);
            System.out.println("methodA的TP90为：" + methodListA.get(methodA_T90.intValue()));
            System.out.println("methodA的TP99为：" + methodListA.get(methodA_T99.intValue()));
        }

        if (methodListB.size() == 0) {
          System.out.println("methodB未调用，暂不处理！");
        } else if (methodListB.size() > 1024) {
            methodListB.clear();
        } else {
            Double methodB_T90 = (methodListB.size() - 1) * 0.9;
            Double methodB_T99 = (methodListB.size() - 1) * 0.99;

            // 排序
            CollectionUtils.sort(methodListB);
            System.out.println("methodB的TP90为：" + methodListB.get(methodB_T90.intValue()));
            System.out.println("methodB的TP99为：" + methodListB.get(methodB_T99.intValue()));
        }

        if (methodListC.size() == 0) {
            System.out.println("methodC未调用，暂不处理！");
        } else if (methodListC.size() > 1024) {
            methodListC.clear();
        } else {
            Double methodC_T90 = (methodListC.size() - 1) * 0.9;
            Double methodC_T99 = (methodListC.size() - 1) * 0.99;

            // 排序
            CollectionUtils.sort(methodListC);
            System.out.println("methodC的TP90为：" + methodListC.get(methodC_T90.intValue()));
            System.out.println("methodC的TP99为：" + methodListC.get(methodC_T99.intValue()));
        }

        System.out.println("接口的响应性能监控分析 end");
    }
}
